<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
    var option={
        type:"get",
        url:"02.php",
        // datatype:"json",
        data:{
           user:"aa",
           pwd:"aa1" 
        },
        callback:function(a){
            console.log(a)
        }
    }      
    // get(option);

    function get(obj={type:"get",data:""}){
    var xhr=new XMLHttpRequest
    var data=""
    if(typeof(obj.data)=="object"){
        for(var key in obj.data){
           data += key+"="+obj.data[key]+"&"
        }
        data=data.substr(0,data.length-1)
    }
    else{
        data=obj.data;
    }
    xhr.open(obj.type,obj.url+"?"+data,true)
    xhr.send()
    xhr.onreadystatechange=function(){
        if(xhr.readyState==4&&xhr.status==200){
            var result=xhr.responseText
            if(obj.datatype=="json"){
                result=JSON.parse(result)
            }
            if(obj.callback){
                obj.callback(result)
            }
        }
    }
    }

    var option1={
        type:"post",
        url:"02.php",
        // datatype:"json",
        data:{
           user:"aa",
           pwd:"aa1" 
        },
        callback:function(a){
            console.log(a)
        }
    } 
    post(option1)
    function post(obj){
        var xhr=new XMLHttpRequest
        var data=""
        if(typeof(obj.data)=="object"){
            for(var key in obj.data){
                data += key+"="+obj.data[key]+"&"
            }
            data=data.substr(0,data.length-1)
        }
        else{
            data=obj.data;
        }
        xhr.open(obj.type,obj.url,true)
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.send(data)
        xhr.onreadystatechange=function(){
        if(xhr.readyState==4&&xhr.status==200){
            var result=xhr.responseText
            if(obj.datatype=="json"){
            var result=JSON.parse(result)
            }
        if(obj.callback){
            obj.callback(result)
            }
        }
    }
    }

    // 封装在$对象中（get,post,ajax）
    var $={
        get: function get(obj={type:"get",data:""}){
        var xhr=new XMLHttpRequest
        var data=""
        if(typeof(obj.data)=="object"){
            for(var key in obj.data){
            data += key+"="+obj.data[key]+"&"
            }
            data=data.substr(0,data.length-1)
        }
        else{
            data=obj.data;
        }
        xhr.open(obj.type,obj.url+"?"+data,true)
        xhr.send()
        xhr.onreadystatechange=function(){
            if(xhr.readyState==4&&xhr.status==200){
                var result=xhr.responseText
                if(obj.datatype=="json"){
                    result=JSON.parse(result)
                }
                if(obj.callback){
                    obj.callback(result)
                }
            }
        }
        },
        post:function post(obj){
            var xhr=new XMLHttpRequest
            var data=""
            if(typeof(obj.data)=="object"){
                for(var key in obj.data){
                    data += key+"="+obj.data[key]+"&"
                }
                data=data.substr(0,data.length-1)
            }
            else{
                data=obj.data;
            }
            xhr.open(obj.type,obj.url,true)
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xhr.send(data)
            xhr.onreadystatechange=function(){
            if(xhr.readyState==4&&xhr.status==200){
                var result=xhr.responseText
                if(obj.datatype=="json"){
                var result=JSON.parse(result)
                }
            if(obj.callback){
                obj.callback(result)
                }
            }
            }
        },
        ajax:function ajax(obj={type:"get",data:""}){
            var xhr=new XMLHttpRequest
            var data=""
            if(typeof(obj.data)=="object"){
                for(var key in obj.data){
                    data += key+"="+obj.data[key]+"&"
                }
                data=data.substr(0,data.length-1)
                obj.data=data
            }
            if(obj.type=="post"){
            xhr.open(obj.type,obj.url,true)
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xhr.send(data)
            }
            else{
            xhr.open(obj.type,obj.url+"?"+data,true)
            xhr.send()   
            }
            xhr.onreadystatechange=function(){
            if(xhr.readyState==4&&xhr.status==200){
                var result=xhr.responseText
                if(obj.datatype=="json"){
                var result=JSON.parse(result)
                }
            if(obj.callback){
                obj.callback(result)
                }
            }
            }
        }
    }
    </script>
</body>
</html>